/**
 * Copyright (c) 2022 Beijing Jiaotong University
 * PhotLab is licensed under [Open Source License].
 * You can use this software according to the terms and conditions of the [Open Source License].
 * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *
 * See the [Open Source License] for more details.
 *
 * Author: Zhenjie Wei
 * Created: Jul. 26, 2023
 * Supported by: National Key Research and Development Program of China
 */

export const keydownManager = new class keydownManager {
    private _callbacks: Map<string, Array<() => void>>

    constructor() {
        this._callbacks = new Map()
    }

    public register(key: string, callback: () => void) {
        if (!this._callbacks.has(key)) {
            this._callbacks.set(key, [])
        }
        this._callbacks.get(key)?.push(callback)
    }

    public invoke(key: string) {
        if (this._callbacks.has(key)) {
            this._callbacks.get(key)?.forEach(callback => {
                callback()
            })
        }
    }
}